function escape(str){
  return str.replace(/\\/g,"\\\\").replace(/\[/g,"\\[").replace(/\]/g,"\\]");
}

function escapeEnable(){
  document.getElementById("escape").disabled=false;
}

var timer;
function escapeDisable(e){
  timer=window.setTimeout("document.getElementById('escape').disabled=true;",200);
}


function escapeSelectedText(){
  clearTimeout(timer);
  var textarea=document.getElementById("edit").data;
  var ss=textarea.selectionStart;
  var se=textarea.selectionEnd;
  var before=textarea.value.substring(ss,se);
  var after=escape(before);
  textarea.value=textarea.value.substring(0,ss)+after+textarea.value.substring(se);
  textarea.setSelectionRange(ss,se-before.length+after.length);
  textarea.focus();
  return;
}
